package com.nexttwist.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

public class BaseDAO {
	protected static SQLiteDatabase database;
	private static SqlLiteHelper dbHelper;

	public synchronized static void openDatabaseRead(Context context) {
		if (!isDatabaseOpen()) {
			// create the db helpers
			if (dbHelper == null) {
				dbHelper = new SqlLiteHelper(context);
			}

			// get the databases
			if (database == null) {
				database = dbHelper.getWritableDatabase();
			}
		}
	}

	public synchronized static boolean isDatabaseOpen() {
		return database != null;
	}

	/** Closes the database. */
	public synchronized static void closeDatabaseRead() {
		// the db helper closes the underlying database
		if (isDatabaseOpen()) {
			database.close();
		}
		dbHelper = null;
		database = null;
	}
}
